﻿using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace com.fukefu.source
{
    //改成单例模式
    public class CookieFactory
    {
        private static CookieFactory ckf = null;
        private static object syncObject = new object();

        private CookieFactory()
        {

        }
        //实例化
        public static CookieFactory GetInstance(bool IsShared)
        {
            if (!IsShared)
            {
                return new CookieFactory();
            }
            else
            {
                return GetInstance();
            }
        }
        public static CookieFactory GetInstance()
        {
            if (ckf == null)
            {
                lock (syncObject)
                {
                    if (ckf == null)
                    {
                        ckf = new CookieFactory();
                    }
                }
            }
            return ckf;
        }

        Hashtable hs = new Hashtable();

        public void Add(string url, string cookies)
        {
            if (string.IsNullOrEmpty(cookies))
            {
                return;
            }
            //抛去时间的干扰
            cookies = Regex.Replace(cookies, "Expires=.+?GMT;?", "", RegexOptions.IgnoreCase);
            //划分片段
            string[] str = cookies.Split(',');
            string cookie = "", domain = "";
            foreach (string s in str)
            {
                if (s.IndexOf("=delete") != -1)
                {
                    continue;
                }
                //获取域名
                cookie = s.Trim() + ";";
                if (cookie.ToLower().IndexOf("domain=") != -1)
                {
                    domain = Regex.Match(cookie, "domain=([^;^\\s]+)", RegexOptions.IgnoreCase).Groups[1].Value.ToString();
                    if (domain.StartsWith("."))
                    {
                        domain = domain.Substring(1);
                    }
                }
                else
                {
                    //为空则设置成顶级域名
                    domain = MyFunction.GetHost(url);
                }
                //判断这个域名cookie是否存在
                Hashtable hs_content;
                if (!hs.Contains(domain))
                {
                    hs_content = new Hashtable();
                }
                else
                {
                    hs_content = hs[domain] as Hashtable;
                }
                //设置本cookie的数值
                GroupCollection gc1 = Regex.Match(cookie, "([^=]+)=([^;]+)").Groups;
                hs_content[gc1[1].Value] = gc1[2].Value;
                //添加到数组
                hs[domain] = hs_content;
            }
        }
        public string Get(string url)
        {
            string cookie = "";
            string host = MyFunction.GetHost(url);
            foreach (DictionaryEntry de in hs)
            {
                if (host.EndsWith(de.Key.ToString()))
                {
                    Hashtable hs_cont = de.Value as Hashtable;
                    foreach (DictionaryEntry kv in hs_cont)
                    {
                        cookie += (cookie.Equals("") ? "" : "; ") + kv.Key + "=" + kv.Value;
                    }
                }
            }
            return cookie;
        }
        //获取制定站点的指定cookie
        public string GetCookie(string url, string cookieName)
        {
            //先查找顶级域名，在查找二级域名
            Hashtable hs_cont = hs[MyFunction.GetDomain(url)] as Hashtable;
            if (hs_cont != null && hs_cont[cookieName] != null)
            {
                return hs_cont[cookieName] as string;
            }
            else
            {
                hs_cont = hs[MyFunction.GetHost(url)] as Hashtable;
                if (hs_cont != null)
                {
                    return hs_cont[cookieName] as string;
                }
            }
            return null;
        }
        //清除一个站点cookie
        public void Clear(string _url)
        {
            string domain = MyFunction.GetDomain(_url);
            lock (hs)
            {
                List<object> _list = new List<object>();
                //找到符合条件的
                foreach (DictionaryEntry de in hs)
                {
                    if (de.Key.ToString().EndsWith(domain))
                    {
                        _list.Add(de.Key);
                    }
                }
                //删除符合条件的
                foreach (object _obj in _list)
                {
                    hs[_obj] = new Hashtable();
                }
            }
        }
        //删除全部cookie
        public void ClearAllCookie()
        {
            hs = new Hashtable();
        }
    }
}
